package com.woniu.controller;

import com.woniu.entity.Emp;
import com.woniu.entity.PageInfo;
import com.woniu.service.EmpService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("emp")
public class EmpController {
    @Resource
    private EmpService empService;
    @GetMapping("listAjax")
    @ResponseBody
    public Map<String,Object> listAjax(PageInfo pageInfo){

        Map<String,Object> resultMap = new HashMap<>();
        try {
            List<Emp> empList = empService.listByPage(pageInfo);
            resultMap.put("empList",empList);
            resultMap.put("pageInfo",pageInfo);
            resultMap.put("result",true);
        }catch (Exception e){
            resultMap.put("result",false);
        }
        return resultMap;
    }

    @PostMapping("deleteSelect")
    @ResponseBody
    public String deleteSelect(@RequestParam List<Integer> empnoes){
        int i =empService.deleteSelect(empnoes);
        System.out.println("删除完毕"+i);
        return "EmpList";
    }

    @PostMapping("findById")
    public String findById(Integer id, Model model){
        Emp emp = empService.findById(id);
        model.addAttribute("emp",emp);
        return "update";
    }

    @PostMapping("findByName")
    @ResponseBody
    public Emp findByName(String ename){
        Emp emp1 = new Emp();
        emp1.setEname(ename);
        Emp emp = empService.findByName(emp1);
        System.out.println(emp);
//        model.addAttribute("emp",emp);
        return emp;
    }

    @PostMapping("changemoney")
    public String   changeMoney(Integer empno,Integer money){
        empService.updateMoney(empno,money);
        return "EmpList";
    }
    @PostMapping("changeDept")
    public String changeDept(Integer empno,Integer deptno){
        empService.updateDept(empno,deptno);
        return "EmpList";
    }
}
